<template>
  <view>
    <goods-list :Goods="Goods" />
    <view v-if="buttomTextIshow">到底了</view>
  </view>
</template>
<script>
import GoodsList from "../../components/goods-list/";
export default {
  components: {
    "goods-list": GoodsList,
  },
  data() {
    return {
      Goods: [],
      initShopImg: [],
      pageNum: 1,
      buttomTextIshow: false,
    };
  },
  onLoad(options) {
    this.initShopImg = JSON.parse(options.data);
    this.getGoods(this.pageNum, this.initShopImg); //接受跳转传过来的初始化图片数组
  },
  onReachBottom() {
    //页面上拉刷新
    this.pageNum = this.pageNum + 1;
    this.getGoods(this.pageNum, this.initShopImg);
  },
  methods: {
    async getGoods(num = 1, initShopImg) {
      console.log(num, initShopImg);
      //获取商品推荐列表
      try {
        const res = await this.$myRequest({
          url: `/api/getgoods?pageindex=${num}`,
        });
        //当请求的接口没有数据的时候，将底部文字显示
        if (res.data.message.length == 0) return (this.buttomTextIshow = true);
        let result = res.data.message || [];
        initShopImg.forEach((img) => {
          result.map((items) => (items.img_url = img));
        });
        for (let i = 0; i < result.length; i++) {
          i < initShopImg.length //当接口数组大于初始化数组时，从头复制给接口数组
            ? (result[i].img_url = initShopImg[i])
            : (result[i].img_url = initShopImg[i - initShopImg.length]);
        }
        console.log(result);
        this.Goods = [...this.Goods, ...result];
      } catch {}
    },
  },
};
</script>
<style>
</style>